<?php

namespace App\Http\Controllers\Admin;

use App\Exports\HousingsNumExport;
use App\Http\Filters\HousingsFilter;
use App\Http\Filters\HousingsNumFilter;
use App\Http\Resources\HousingsResource;
use App\Models\Housings;
use App\Models\HousingsNum;
use App\Http\Requests\HousingsNumRequest;
use App\Http\Resources\HousingsNumResource;
use Illuminate\Http\Request;
use Maatwebsite\Excel\Facades\Excel;
use App\Models\Report;

class HousingsNumController extends Controller
{
    public function index(HousingsNumFilter $filter)
    {
        $housingsNums = HousingsNum::query()->with('housings')
            ->filter($filter)
            ->paginate();

        return $this->ok(HousingsResource::collection($housingsNums));
    }

    public function create()
    {
        return $this->ok();
    }

    public function store(HousingsNumRequest $request)
    {
        $inputs = $request->validated();
        $housingsNum = HousingsNum::create($inputs);

        return $this->created(HousingsNumResource::make($housingsNum));
    }

    public function edit(Request $request, HousingsNum $housingsNum)
    {
        return $this->ok(HousingsNumResource::make($housingsNum));
    }

    public function update(HousingsNumRequest $request, HousingsNum $housingsNum)
    {
        $inputs = $request->validated();
        $housingsNum->update($inputs);

        return $this->created(HousingsNumResource::make($housingsNum));
    }

    public function destroy(HousingsNum $housingsNum)
    {
        $housingsNum->delete();
        return $this->noContent();
    }

    public function export(Request $request)
    {

        $filters = $request->only([
            'ids',
            // ... 其他可能的过滤条件
        ]);
        return Excel::download(new HousingsNumExport($filters), '房源码.xlsx');
    }
}
